from mpmath import mp

import util
from utli_2.blink_thresh import BlinkThresh


# from blink_thresh import BlinkThresh
class BlinkDetect:
    def __init__(self):
        self.face_mesh = mp.solutions.face_mesh.FaceMesh()
# 存储关键点
        self.landmark = []
# 左眼关键点序号
        self.FACEMESH_LEFT_EYE = [362, 385, 387, 263, 373, 380]
# 右眼关键点序号
        self.FACEMESH_RIGHT_EYE = [33, 160, 158, 133, 153, 144]
# # 初始化的眨眼阈值
        self.blink_thresh = BlinkThresh()
        pass
def cal_EAR(self, frame):

    p2 = self.idxCvtPoint(frame, 160)
    p6 = self.idxCvtPoint(frame, 144)
    p26 = util.getDistance(p2, p6)
    p3 = self.idxCvtPoint(frame, 158)
    p5 = self.idxCvtPoint(frame, 153)
    p35 = util.getDistance(p3, p5)
    p1 = self.idxCvtPoint(frame, 33)
    p4 = self.idxCvtPoint(frame, 133)
    p14 = util.getDistance(p1, p4)
    EAR = (p26 + p35)/(2*p14) * 10
# 统计纵横比
    avg_thresh = self.blink_thresh.get_thresh(EAR)
    if avg_thresh < 0:
        print("统计中....")
        return
# 计算10帧EAR的平均值
    thresh = avg_thresh
# 统计眨眼的次数
    if EAR < thresh * 0.85:
        print("眨眼")
        print(EAR)
    pass